from transformers import AutoModelForCausalLM, AutoTokenizer

checkpoint = "Qwen/Qwen1.5-1.8B-Chat"
model = AutoModelForCausalLM.from_pretrained(checkpoint, device_map="auto")
tokenizer = AutoTokenizer.from_pretrained(checkpoint)

prompt = "给我讲一个笑话。"
messages = [
    {"role": "system", "content": "你是一个AI助手。"},
    {"role": "user", "content": prompt}
]
text = tokenizer.apply_chat_template(
    messages,
    tokenize=False,
    add_generation_prompt=True
)
print(text)

inputs = tokenizer(text, return_tensors="pt").to(model.device)
outputs = model.generate(**inputs, 
    max_new_tokens=1024)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))